package com.wtp.wallpaper.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.wtp.wallpaper.pojo.dto.ScoreDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RatingMessageConsumer {

    @Autowired
    private StringRedisTemplate redisTemplate;

    // 接收消息并存储到 Redis
//    public void receiveRatingMessage(ScoreDTO scoreDTO) {
//
//        log.info("消费者消费：{}",scoreDTO);
//
//        String wallpaperId = "wallpaper:" + scoreDTO.getPicId();
//        // 使用 Redis 的原子操作更新评分和计数
//        redisTemplate.opsForValue().increment(wallpaperId + ":totalRating", scoreDTO.getScore());
//        redisTemplate.opsForValue().increment(wallpaperId + ":ratingCount", 1);
//    }

    public void receiveRatingMessage(String scoreDTOStr) {

        log.info("消费者消费：{}",scoreDTOStr);
        ScoreDTO scoreDTO = JSON.parseObject(scoreDTOStr,ScoreDTO.class);
        log.info("消费者消费：{}",scoreDTO);

        String wallpaperId = "wallpaper:" + scoreDTO.getPicId();
        // 使用 Redis 的原子操作更新评分和计数
        redisTemplate.opsForValue().increment(wallpaperId + ":totalRating", scoreDTO.getScore());
        redisTemplate.opsForValue().increment(wallpaperId + ":ratingCount", 1);
    }
}
